<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>新闻爬取</title>
    <link rel="stylesheet" type="text/css" href="../../static/layui/css/layui.css">
    <link rel="stylesheet" type="text/css" href="../../static/news/css/main.css">
    <script type="text/javascript" src="../../static/jquery-3.4.1.js"></script>
    <script type="text/javascript" src="../../static/layui/layui.js"></script>
</head>
<body>
<div class="layui-tab">
  <ul class="layui-tab-title">
    <li class="layui-this">自动爬取</li>
    <li>手动爬取</li>
  </ul>
  <div class="layui-tab-content" style="margin-top: 20px">
    <div class="layui-tab-item layui-show">
        <form class="layui-form">
            <div class="layui-form-item">
                <label class="layui-form-label">爬取数量</label>
                <div class="layui-input-block">
                  <input type="text" id="number" name="number" required  lay-verify="required" placeholder="请输入数量" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                  <button class="layui-btn" lay-submit lay-filter="submitButton">开始爬取</button>
                  <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
        </form>
        <ul class="article_list" id="article_list">

        </ul>
  </div>
    <div class="layui-tab-item">
        <form class="layui-form">
            <div class="layui-form-item">
                <label class="layui-form-label">新闻链接</label>
                <div class="layui-input-block">
                  <input type="text" name="news_link" required  lay-verify="required" placeholder="请输入链接" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                  <button class="layui-btn" lay-submit lay-filter="submitButton2">开始爬取</button>
                  <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
        </form>
        <ul class="article_list" id="article_list2">

        </ul>
    </div>
  </div>
</div>
</body>

<script>
layui.use('element', function(){
  var element = layui.element;
});
layui.use('form', function(){
    var form = layui.form; //只有执行了这一步，部分表单元素才会自动修饰成功
    form.on('submit(submitButton)',function (data) {
        data.field['action']='automatic'
        var maintenancePlace = JSON.stringify(data.field);
        console.log(maintenancePlace)
        console.log(maintenancePlace);
        $.ajax({
            url : '/profile/crawler',
            type : 'post',
            dataType : 'json',
            contentType :"application/json",
            data : maintenancePlace,
            success : function (resp) {
                alert("成功爬取："+resp.a2+" 爬取失败："+resp.a1)
                $("#article_list").html("");
                for (var i = 0; i < resp.news.length; i++) {
                    var news = resp.news[i];
                    var html = '<li><a href="/news/' + news.id + '" target="_blank">' + news.title + '</a><span>' + news.create_time + '</span></li>'
                    // 添加数据
                    $("#article_list").append(html)
                }
            }
        })
        return false;
    });
    form.on('submit(submitButton2)',function (data) {
        data.field['action']='manual'
        var maintenancePlace = JSON.stringify(data.field);
        console.log(maintenancePlace);
        $.ajax({
            url : '/profile/crawler',
            type : 'post',
            dataType : 'json',
            contentType :"application/json",
            data : maintenancePlace,
            success : function (resp) {
                if (resp.errno == "0") {
                    alert("成功爬取" )
                    $("#article_list2").html("");
                    for (var i = 0; i < resp.news.length; i++) {
                        var news = resp.news[i];
                        var html = '<li><a href="/news/' + news.id + '" target="_blank">' + news.title + '</a><span>' + news.create_time + '</span></li>'
                        // 添加数据
                        $("#article_list2").append(html)
                    }
                }
                else {
                    // 爬取失败
                    alert(resp.errmsg);
                }
            }
        })
        return false;
    })
});
</script>
</html>